{{- /*
GitHub repository.

@param {string} (positional parameter 0) Repository name on GitHub.

@returns {template.HTML}

@examples

  {{< gh-repo "github/docs" >}}
*/ -}}

{{- if (.Get 0) -}}

    {{ $repos := (split (.Get 0) "|") }}
    {{ $api := (site.Params.api) }}
    {{ $ts := (now.Unix) }}
    {{ $headers := (dict
    "User-Agent" ($api.user_agent)
    "Authorization" (printf "Bearer %s" (($api.github.token) | base64Decode))
    ) }}

    <div class="shortcode shortcode-{{ ($.Name) }} mb-3">
      <div class="row row-cols-1 row-cols-lg-2 g-3">
          {{ range ($repos) }}
              {{ $repo := (getJSON ($api.github.url) "/repos/" . "?ts=" ($ts) ($headers)) }}
              {{ $owner := (getJSON ($repo.owner.url) "?ts=" ($ts) ($headers)) }}
              <div class="col">
                <div class="card">
                  <div class="row g-0">
                      {{ if ($repo.owner.avatar_url) }}
                        <div class="col-md-4 p-1">
                          <a class="d-block" href="{{ ($repo.html_url) }}" target="_blank" rel="noopener nofollow">
                            <img class="card-img"
                                 src="{{ ($repo.owner.avatar_url) }}"
                                 alt="{{ ($repo.name) }}" loading="lazy"/>
                          </a>
                        </div>
                      {{ end }}
                    <div class="col">
                      <div class="card-body h-100">
                        <div class="d-flex flex-column h-100">
                          <div class="flex-shrink-0">
                            <h5 class="card-title">
                              <a href="{{ ($repo.html_url) }}" target="_blank" rel="noopener nofollow">
                                  {{ ($repo.name) }}
                              </a>
                            </h5>
                            <h6 class="card-subtitle mb-2 text-body-secondary">
                                {{ (($owner.name) | default ($owner.login)) }}
                            </h6>
                              {{ if ($repo.description) }}
                                <p class="card-text">{{ (($repo.description) | plainify) }}</p>
                              {{ end }}
                          </div>
                          <div class="mt-auto">
                            <div class="row row-cols-auto g-2 mt-3">
                              <div class="col" data-bs-tooltip data-bs-title='{{ (i18n "sc.github.repo.subscribers") }}'>
                                <a class="btn btn-light btn-sm text-body-secondary" href="{{ ($repo.html_url) }}/watchers"
                                   target="_blank" rel="noopener nofollow"
                                   role="button" aria-label='{{ (i18n "sc.github.repo.subscribers") }}'>
                                  <i class="fas fa-eye fa-fw me-1"></i>
                                    {{ ($repo.subscribers_count) }}
                                </a>
                              </div>
                              <div class="col" data-bs-tooltip data-bs-title='{{ (i18n "sc.github.repo.stargazers") }}'>
                                <a class="btn btn-light btn-sm text-body-secondary" href="{{ ($repo.html_url) }}/stargazers"
                                   target="_blank" rel="noopener nofollow"
                                   role="button" aria-label='{{ (i18n "sc.github.repo.stargazers") }}'>
                                  <i class="fas fa-star fa-fw me-1"></i>
                                    {{ ($repo.stargazers_count) }}
                                </a>
                              </div>
                              <div class="col" data-bs-tooltip data-bs-title='{{ (i18n "sc.github.repo.forks") }}'>
                                <a class="btn btn-light btn-sm text-body-secondary" href="{{ ($repo.html_url) }}/network/members"
                                   target="_blank" rel="noopener nofollow"
                                   role="button" aria-label='{{ (i18n "sc.github.repo.forks") }}'>
                                  <i class="fas fa-code-branch fa-fw me-1"></i>
                                    {{ ($repo.forks_count) }}
                                </a>
                              </div>
                                {{ if ($repo.license) }}
                                    {{ $license := (getJSON ($repo.license.url) "?ts=" ($ts) ($headers)) }}
                                    <div class="col" data-bs-tooltip data-bs-title='{{ (i18n "sc.github.repo.license") }}'>
                                      <a class="btn btn-light btn-sm text-body-secondary" href="{{ ($license.html_url) }}"
                                         target="_blank" rel="noopener nofollow"
                                         role="button" aria-label='{{ (i18n "sc.github.repo.license") }}'>
                                        <i class="fas fa-copyright fa-fw me-1"></i>
                                          {{ ($license.spdx_id) }}
                                      </a>
                                    </div>
                                {{ end }}
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
          {{ end }}
      </div>
    </div>

{{- else -}}

    {{ (partial "alerts/no_params" .) }}

{{- end -}}
